﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using InfoPick.UI.ViewModel;
using System.Xml.Linq;
using InfoPick.UI.Comm;
using Telerik.Windows.Controls;
using InfoPick.Lib.Service;

namespace InfoPick.UI.Views
{
    /// <summary>
    /// Interaction logic for MGW.xaml
    /// </summary>
    public partial class MGWPage : Page
    {
        public MGWPage()
        {
            InitializeComponent();
            init();
        }

        void checkForm()
        {
            NameBox.GetBindingExpression(RadMaskedTextBox.ValueProperty).UpdateSource();
            AddressBox.GetBindingExpression(RadMaskedTextBox.ValueProperty).UpdateSource();
            PortBox.GetBindingExpression(RadMaskedTextBox.ValueProperty).UpdateSource();
            UserBox.GetBindingExpression(RadMaskedTextBox.ValueProperty).UpdateSource();
            tbPsd.GetBindingExpression(PasswordBoxHelper.PasswordProperty).UpdateSource();
        }

        ServerService ser = new ServerService("MGWS");
        List<ServerModel> list = new List<ServerModel>();
        void init()
        {
            ServerInfo.Opacity = 0.3;
            ServerInfo.IsEnabled = false;
            ServerInfo.DataContext = null;

            list.Clear();
            list.AddRange(ser.LoadList().Select(c => ServerModel.FormServer(c)));

            lb.ItemsSource = list;
            lb.Items.Refresh();
        }

        private void Refresh_Click(object sender, RoutedEventArgs e)
        {
            init();
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            ServerInfo.IsEnabled = true;
            ServerInfo.Opacity = 1;
            NameBox.Focus();
            ServerInfo.DataContext = new ServerModel();
            checkForm();
        }

        private void del_Click(object sender, RoutedEventArgs e)
        {
            if (lb.SelectedItem != null)
            {
                ServerModel obj = lb.SelectedItem as ServerModel;
                if (obj != null)
                {
                    Tools.OpenConfirm("确认要删除选中的数据？", new EventHandler<WindowClosedEventArgs>((object o, WindowClosedEventArgs ex) =>
                    {
                        if (ex.DialogResult != null && ex.DialogResult.Value)
                        {
                            ser.Delete(obj.Guid);
                            init();

                        }
                    }));
                    
                }
            }
        }

        private void save_Click(object sender, RoutedEventArgs e)
        {
            if (Validation.GetHasError(NameBox) || Validation.GetHasError(AddressBox) || Validation.GetHasError(PortBox)
                || Validation.GetHasError(UserBox) || Validation.GetHasError(tbPsd))
                return;

            ServerModel obj = ServerInfo.DataContext as ServerModel;
            if (obj != null)
            {
                ser.SaveOrUpdate(obj.ToServer());

                init();
            }
        }

        private void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                ServerInfo.IsEnabled = true;
                ServerInfo.Opacity = 1;
                ServerModel obj = e.AddedItems[0] as ServerModel;
                if (obj != null)
                {
                    var cpyObj = obj.Copy();
                    ServerInfo.DataContext = cpyObj;
                    checkForm();
                }
            }
        }

        private void searchTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            string key = (sender as RadWatermarkTextBox).Text.Trim().ToLower();
            if (key.Length > 0)
            {
                lb.ItemsSource = list.Where(c => c.Name.ToLower().Contains(key)).ToList();
            }
            else
            {
                lb.ItemsSource = list;
            }
        }
    }
}
